package com.un.ebs.product.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.product.domain.ProductSaleClass;
import com.un.ebs.product.dto.ProductSaleClassDto;
import com.un.ebs.product.service.ProductSaleClassService;
import com.un.ebs.sys.dto.ObjectParentAndIndexChangeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-16
 */
@RestController
@RequestMapping("/productSaleClass")
@Api(value = "product-sale-class", description = "销售品类")
public class ProductSaleClassController {

    @Resource
    private ProductSaleClassService service;

    @PostMapping("save")
    @ApiOperation(value = "保存，新增或修改一个类别信息")
    public WebResult update(@RequestBody @Validated ProductSaleClassDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @PostMapping("addByProduct")
    @ApiOperation(value = "添加，把商品添加到指定的小类中")
    public WebResult addByProduct(@RequestParam String smallClassId, @RequestParam List<String> productIds) {
        service.addByProduct(smallClassId, productIds);
        return WebUtils.success();
    }

    @PostMapping("removeByProduct")
    @ApiOperation(value = "删除，把商品从销售类别中移除")
    public WebResult removeByProduct(@RequestParam String smallClassId, @RequestParam List<String> productIds) {
        service.removeByProduct(smallClassId, productIds);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情", notes = "根据主键获取单个 ProductSaleClass")
    public WebResult<ProductSaleClass> getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询，所有类别", notes = "查询当前租户所有品类的一级分类")
    public WebResult<List<ProductSaleClass>> list() {
        return WebUtils.success(service.queryAll());
    }

    @GetMapping("listRoot")
    @ApiOperation(value = "查询，仅包括一级分类", notes = "查询当前租户所有品类的一级分类")
    public WebResult<List<ProductSaleClass>> listRoot() {
        return WebUtils.success(service.queryParent());
    }

    @PostMapping("changeParent")
    @ApiOperation(value = "变更，修改类别的父级", notes = "修改 ProductClass 的 parentId 属性")
    public WebResult changeParent(@RequestParam String id, @RequestParam String parentId) {
        service.changeParent(id, parentId);
        return WebUtils.success();
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "变更，修改排序", notes = "修改 ProductClass 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

    @PostMapping("changeNode")
    @ApiOperation(value = "批量变更，父级或排序", notes = "批量修改节点中的父级对象和排序")
    public WebResult changeNode(@RequestBody List<ObjectParentAndIndexChangeDto> nodes) {
        service.changeNode(nodes);
        return WebUtils.success();
    }

    @GetMapping("/child/{parentId}")
    @ApiOperation(value = "查询，所有子类", notes = "查询指定 parentId 属性的 ProductSaleClass")
    public WebResult<List<ProductSaleClass>> listChild(@PathVariable("parentId") String parentId) {
        return WebUtils.success(service.queryByParentId(parentId));
    }

    @GetMapping("/childPage/{parentId}/{pageSize}/{pageIndex}")
    @ApiOperation(value = "查询，所有子类，带分页", notes = "查询指定 parentId 属性的 ProductClass")
    public WebResult<IPage<ProductSaleClass>> listPageChild(@PathVariable("parentId") String parentId, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) {
        return WebUtils.success(service.queryPageByParentId(parentId, pageSize, pageIndex));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 ProductClass")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

}